import { defineStore } from 'pinia';

interface Token {
   token?: string;
   refreshToken?: string;
}

interface UserInfo {}

interface UserState {
   token: Token | null;
   userInfo: any | null;
}

export const useUserStore = defineStore('user', {
   state: (): UserState => ({
      token: uni.getStorageSync('token'),
      userInfo: uni.getStorageSync('userInfo'),
   }),

   getters: {
      getToken: state => state.token,
      getUserInfo: state => state.userInfo,
   },

   actions: {
      // 设置 token
      setToken(token: Token) {
         this.token = token;
         uni.setStorageSync('token', token);
      },

      // 设置用户信息
      setUserInfo(userInfo: UserInfo) {
         this.userInfo = userInfo;
         uni.setStorageSync('userInfo', userInfo);
      },

      // 登录
      login(token: Token) {
         this.setToken(token);
      },

      // 登出
      logout() {
         this.token = null;
         this.userInfo = null;
         uni.removeStorageSync('token');
         uni.removeStorageSync('userInfo');
      },
   },
});
